package com.xtm.exercise.utils;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

public class CheckFormatUtil {

	public static void errorFormat(String msg, List<String> errors) {
		errors.add("\"" + msg + "信息有误\"");
	}

	public static void errorOptionalVehiclesFormat(String msg, List<String> errors) {
		errors.add("\"" + msg + "不存在待选车辆列表\"");
	}

	public static void errorColorVehiclesFormat(String msg, List<String> errors) {
		errors.add("\"" + msg + "车牌颜色与车牌号码不符\"");
	}

	public static void doesNotEmpty(String msg, List<String> errors) {
		errors.add("\"" + msg + "不能为空\"");
	}

	public static void errorInput(String msg, List<String> errors) {
		errors.add("\"" + msg + "输入错误\"");
	}
	
	public static void errorRepeat(String msg, List<String> errors) {
		errors.add("\"" + msg + "不仅有一家单位与之匹配\"");
	}
	
	public static void errorExists(String msg, List<String> errors) {
		errors.add("\"" + msg + "已存在，不能重复\"");
	}
	
	public static void outOfRange(String msg, List<String> errors) {
		errors.add("\"" + msg + "超出数据范围\"");
	}
	
	public static void doesEmpty(String msg, List<String> errors) {
		errors.add("\"" + msg + "必须为空\"");
	}
	
	public static void notFoundVehicleId(String msg, List<String> errors) {
        errors.add("\"" + msg + "未找到匹配台账记录\"");
    }
	
	public static void notFoundDriver(String msg, List<String> errors) {
        errors.add("\"" + msg + "未找到对应的驾驶员\"");
    }

	public static String getMessage(int line, String str) {
		return "第" + line + "行的" + str;
	}
	
	public static void doesNotEnoughColumn(String msg, List<String> errors) {
        errors.add("\"" + msg + "数据列数与模板数据列数不符\"");
    }
	public static void doesNotSelectInsurance(String msg,List<String> errors){
	    errors.add("\"" + msg + "请至少选择一种\"");
	}

	// 日期格式
	@SuppressWarnings("finally")
	public static Date formatDate(String str, String formatter, int line, String columnName, List<String> errors) {

		Date date = null;
		try {
			date = DateTimeUtil.strToDate(str, formatter);
		} catch (Exception e) {
			errorFormat(getMessage(line, columnName), errors);
		} finally {
			return date;
		}
	}
	
	public static void checkEmpty(String name, int line, String columnName, List<String> errors) {
		if (StringUtils.isNotEmpty(name)) {
			doesEmpty(getMessage(line, columnName), errors);
			return;
		}
	}
	
	public static void checkNotEmpty(String name, int line, String columnName, List<String> errors) {
		if (StringUtils.isEmpty(name)) {
			doesNotEmpty(getMessage(line, columnName), errors);
			return;
		}
	}

	public static void checkDriverName(String name, int line, String columnName, List<String> errors) {
		if (StringUtils.isEmpty(name)) {
			doesNotEmpty(getMessage(line, columnName), errors);
			return;
		}

		if (!DataCheckUtil.checkDriverName(name)) {
			errorFormat(getMessage(line, columnName), errors);
		}
	}

	public static Timestamp formatTimestamp(String str, int line, String columnName, List<String> errors) {

		Timestamp timestamp = DataCheckUtil.changeToTimestamp(str);
		if (timestamp == null) {
			errorFormat(getMessage(line, columnName), errors);
		}

		return timestamp;
	}

	// 性别
	public static void checkSex(String sex, int line, List<String> errors) {

		if (!DataCheckUtil.checkSex(sex)) {
			errorInput(getMessage(line, "性别"), errors);
		}
	}

	// 电话号码
	public static void checkPhoneNumber(String phone, int line, String columnName, List<String> errors) {

		if (!DataCheckUtil.checkPhoneNumber(phone)) {
			errorFormat(getMessage(line, columnName), errors);
		}
	}

	// 驾驶证号 && 身份证号
	public static void checkLicenseAndId(String license, String idNumber, int line, List<String> errors) {

		if (!DataCheckUtil.checkIdentityNumber(idNumber)) {
			errorFormat(getMessage(line, "身份证号"), errors);
			return;
		}
		/*if (!DataCheckUtil.checkIdentityNumber(license)) {
			errorFormat(getMessage(line, "驾驶证号"), errors);
			return;
		}*/
		if(idNumber.length()!=18){
			errorFormat(getMessage(line, "身份证号"), errors);
		}
		if(license.length()!=15 && license.length()!=18){
			errorFormat(getMessage(line, "驾驶证号"), errors);
		}

		/*if (!license.equals(idNumber)) {
			if (idNumber.length() == 18) {
				if (license.length() != 15) {
					errorFormat(getMessage(line, "驾驶证号"), errors);
				}
			} else if (idNumber.length() == 15) {
				if (license.length() != 18) {
					errorFormat(getMessage(line, "驾驶证号"), errors);
				}
			} else {
				errorFormat(getMessage(line, "身份证号"), errors);
			}
		}*/
	}

	// 手机短号
	public static void checkShortNum(String shortNum, int line, List<String> errors) {

		if (StringUtils.isEmpty(shortNum))
			return;

		if (!DataCheckUtil.checkShortNumber(shortNum)) {
			errorFormat(getMessage(line, "手机短号"), errors);
		}
	}

	public static String checkYesNo(String flg, int line, String columnNm, List<String> errors) {
		if (DataCheckUtil.checkYesNo(flg)) {
			if (StringUtils.isEmpty(flg) || flg.equals("否")) {
				return "0";
			} else {
				return "1";
			}
		} else {
			errorInput(getMessage(line, columnNm), errors);
			return null;
		}
	}
	
//	车牌号码
	public static boolean checkVehicleNumber(String str, int line, String columnName, List<String> errors) {
		if (StringUtils.isEmpty(str)) {
			doesNotEmpty(getMessage(line, columnName), errors);
			return false;
		}

		if (!DataCheckUtil.checkVehicleNumber(str)) {
			errorFormat(getMessage(line, columnName), errors);
			return false;
		}
		return true;
	}
	
//	车架号
	public static void checkFrameNumber(String str, int line, String columnName, List<String> errors) {
		if (StringUtils.isEmpty(str)) {
			doesNotEmpty(getMessage(line, columnName), errors);
			return;
		}

		if (!DataCheckUtil.checkFrameNumber(str)) {
			errorFormat(getMessage(line, columnName), errors);
		}
	}
	
//	含税单价
	public static boolean checkVehiclePrice(String str, int line, String columnName, List<String> errors) {
		if (StringUtils.isEmpty(str)) {
			doesNotEmpty(getMessage(line, columnName), errors);
			return  false;
		}

		if (!DataCheckUtil.isNumber(str)) {
			errorFormat(getMessage(line, columnName), errors);
			return  false;
		}
		
		Float value = new Float(str);
		if(value < 0 || value > 999.99){
			outOfRange(getMessage(line, columnName), errors);
			return  false;
		}
		return true;
	}
	
	
//	新车单价
	public static boolean checkNewPrice(String str, int line, String columnName, List<String> errors) {
		if (StringUtils.isEmpty(str)) {
			doesNotEmpty(getMessage(line, columnName), errors);
			return false;
		}

		if (!DataCheckUtil.isNumber(str)) {
			errorFormat(getMessage(line, columnName), errors);
			return false;
		}
		
		Float value = new Float(str);
		if(value < 0 || value > 999.99){
			outOfRange(getMessage(line, columnName), errors);
			return false;
		}
		return true;
	}
	
	
//	发动机号
	public static void checkEngineNumber(String str, int line, String columnName, List<String> errors) {

		if (!DataCheckUtil.checkEngineNumber(str)) {
			errorFormat(getMessage(line, columnName), errors);
		}
	}
	
//	行驶证号
	public static void checkDrivingPermitNo(String str, int line, String columnName, List<String> errors) {

		if (!DataCheckUtil.checkDrivingPermitNo(str)) {
			errorFormat(getMessage(line, columnName), errors);
		}
	}
	

	
	
//	排量
	public static boolean checkOutputVolume(String str, int line, String columnName, List<String> errors) {
		if (StringUtils.isEmpty(str)) {
			doesNotEmpty(getMessage(line, columnName), errors);
			return  false;
		}

		if (!DataCheckUtil.isNumber(str)) {
			errorFormat(getMessage(line, columnName), errors);
			return  false;
		}
		
		Float value = new Float(str);
		if(value < 0 || value > 99.9){
			outOfRange(getMessage(line, columnName), errors);
			return  false;
		}
		return  true;
	}
	
//	电池容量
	public static boolean checkBatteryVolume(String str, int line, String columnName, List<String> errors) {
		if (StringUtils.isEmpty(str)) {
			doesNotEmpty(getMessage(line, columnName), errors);
			return false;
		}

		if (!DataCheckUtil.isNumber(str)) {
			errorFormat(getMessage(line, columnName), errors);
			return false;
		}
		
		Float value = new Float(str);
		if(value < 0 || value > 99999.99){
			outOfRange(getMessage(line, columnName), errors);
			return false;
		}
		return true;
	}
	
//	核定载客人数
	public static boolean checkFixedNumbers(String str, int line, String columnName, List<String> errors) {
		if (StringUtils.isEmpty(str)) {
			doesNotEmpty(getMessage(line, columnName), errors);
			return false;
		}

		if (!DataCheckUtil.checkFixedNumbers(str)) {
			errorFormat(getMessage(line, columnName), errors);
			return false;
		}
		return true;
	}
	
//	固定资产单位 （社会企业）
	public static void checkOutterName(String str, int line, String columnName, List<String> errors) {
		if (StringUtils.isEmpty(str)) {
			doesNotEmpty(getMessage(line, columnName), errors);
			return;
		}

		if (!DataCheckUtil.checkOutterName(str)) {
			errorFormat(getMessage(line, columnName), errors);
		}
	}
	
//	固定资产卡号
	public static void checkAssetsCard(String str, int line, String columnName, List<String> errors) {
		if (StringUtils.isEmpty(str)) {
			doesNotEmpty(getMessage(line, columnName), errors);
			return;
		}

		if (!DataCheckUtil.checkAssetsCard(str)) {
			errorFormat(getMessage(line, columnName), errors);
		}
	}
	
//	续航里程
	public static boolean checkEnergyMile(String str, int line, String columnName, List<String> errors) {
		if (StringUtils.isEmpty(str)) {
			doesNotEmpty(getMessage(line, columnName), errors);
			return false;
		}

		if (!DataCheckUtil.isNumber(str)) {
			errorFormat(getMessage(line, columnName), errors);
			return false;
		}
		
		Float value = new Float(str);
		if(value < 0 || value > 9999.99){
			outOfRange(getMessage(line, columnName), errors);
			return false;
		}
		return true;
	}
	
//	姓名
	public static void checkName(String name, int line, String columnName, List<String> errors) {
		if (StringUtils.isEmpty(name)){
			doesNotEmpty(getMessage(line, columnName), errors);
			return;
		}
		if (!DataCheckUtil.checkName(name)) {
			errorFormat(getMessage(line, columnName), errors);
		}
	}

//	加油卡编号
	public static void checkOilCard(String name, int line, String columnName, List<String> errors){
		if (StringUtils.isEmpty(name)){
			doesNotEmpty(getMessage(line, columnName), errors);
			return;
		}
	}
	
//	企业负责人职务
	public static void checkOfficailPost(String name, int line, String columnName, List<String> errors) {
		if (StringUtils.isEmpty(name)){
			doesNotEmpty(getMessage(line, columnName), errors);
			return;
		}
		if (!DataCheckUtil.checkOfficailPost(name)) {
			errorFormat(getMessage(line, columnName), errors);
		}
	}
	
//	里程
	public static boolean checkMile(String str, int line, String columnName, List<String> errors) {
		if (StringUtils.isEmpty(str)) {
			doesNotEmpty(getMessage(line, columnName), errors);
			return false;
		}

		if (!DataCheckUtil.isNumber(str)) {
			errorFormat(getMessage(line, columnName), errors);
			return false;
		}
		
		Float value = new Float(str);
		if(value < 0 || value > 999999.99){
			outOfRange(getMessage(line, columnName), errors);
			return false;
		}
		return true;
	}
	
	//二位正整数
	//用车人数 
	public static boolean checkUseNumber(String str, int line, String columnName, List<String> errors) {
	    if (StringUtils.isEmpty(str)) {
            doesNotEmpty(getMessage(line, columnName), errors);
            return false;
        }

        if (!DataCheckUtil.checkUseNumber(str)) {
            errorFormat(getMessage(line, columnName), errors);
            return false;
        }
        return true;

        
	}
	//四位正整数+'.'+二位小数
	//油耗、加油量
	public static boolean checkFourDigitFloatNumber(String str,int line,String columnName,List<String> errors){
	    if (StringUtils.isEmpty(str)) {
            doesNotEmpty(getMessage(line, columnName), errors);
            return false;
        }
        if (!DataCheckUtil.checkFourDigitNumber(str)) {
            errorFormat(getMessage(line, columnName), errors);
            return false;
        }
        return true;
	}
	
	/**
	 * 校验字符长度
	 * @param str
	 * @param maxLen  最大长度
	 * @param line
	 * @param columnName
	 * @param errors
	 */
    public static void checkLength(String str,int maxLen, int line, String columnName, List<String> errors) {

        if (!DataCheckUtil.checkLength(str,maxLen)) {
            errorFormat(getMessage(line, columnName), errors);
        }
        
    }
    //0到999999.99的二位小数的数字  
    //六位整数+'.'+二位小数
    //如果字段是必填项，自己校验，在调此方法
    //成本的加油量、加油金额、
    public static BigDecimal checkFloatNumber(String str, int line, String columnName, List<String> errors){
        if (!DataCheckUtil.checkMoney(str)) {
            errorFormat(getMessage(line, columnName), errors);
            return new BigDecimal(0);
        }else{
            BigDecimal num = new BigDecimal(str);
            return num;
        }
    }
    //二位整数+'.'+二位小数
    //如果字段是必填项，自己校验，在调此方法
    //加油单价
    public static BigDecimal checkTwoDigitFloatNumber(String str, int line, String columnName, List<String> errors){
        if (!DataCheckUtil.checkTwoDigitNumber(str)) {
            errorFormat(getMessage(line, columnName), errors);
            return new BigDecimal(0);
        }else{
            BigDecimal num = new BigDecimal(str);
            return num;
        }
    }
    
    //当选择商业险时，选择的商业险明细校验
    public static String checkTrueFalse(String flg, int line, String columnNm, List<String> errors) {
        if (DataCheckUtil.checkYesNo(flg)) {
            if (StringUtils.isEmpty(flg) || flg.equals("否")) {
                return "";
            } else {
                return "true";
            }
        } else {
            errorInput(getMessage(line, columnNm), errors);
            return "";
        }
    }
    /**
     * 校验特殊字符和长度
     * @param str
     * @param maxLen
     * @param line
     * @param columnName
     * @param errors
     */
    public static boolean checkSpecialTextLength(String str,int maxLen, int line, String columnName, List<String> errors) {
        if (!DataCheckUtil.specialTextLength(str,maxLen)) {
            errorFormat(getMessage(line, columnName), errors);
            return false;
        }
        return true;
        
    }
    /**
     * 维保记录导入的 里程表是否归零
     * @param flg
     * @param line
     * @param columnNm
     * @param errors
     * @return 1：是 2：否
     */
    public static String checkIfMileageZero(String flg, int line, String columnNm, List<String> errors) {
        if (DataCheckUtil.checkYesNo(flg)) {
            if (StringUtils.isEmpty(flg) || flg.equals("否")) {
                return "2";
            } else {
                return "1";
            }
        } else {
            errorInput(getMessage(line, columnNm), errors);
            return null;
        }
    }
}
